package com.example.internetplus.accelerationService.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.internetplus.accelerationService.StressstService;
import com.example.internetplus.controller.globalException.exception.BusinessException;
import com.example.internetplus.mapper.HandStressMapper;
import com.example.internetplus.domain.po.HandStress;
import com.example.internetplus.result.Result;
import com.example.internetplus.utils.MathUtil;
import com.example.internetplus.utils.execlUtil.ExeclUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.example.internetplus.constrants.ExceptionConstants.*;
import static com.example.internetplus.constrants.ExeclConstrants.*;
import static com.example.internetplus.constrants.ResultConstrants.*;

@Transactional
@Service
@RequiredArgsConstructor
public class StressServiceImpl extends ServiceImpl<HandStressMapper,HandStress> implements StressstService {

    private final HandStressMapper handStressMapper;

    private final MathUtil mathUtil;
    @Override
    public Result calacceleration() {
        Result result = queryAll();
        //获取查询到的数据
        List<HandStress> data = (List<HandStress>)result.getData();
        List<Double> a0 = data.stream().map(HandStress::getA0).collect(Collectors.toList());
        List<Double> a1 = data.stream().map(HandStress::getA1).collect(Collectors.toList());
        List<Double> a2 = data.stream().map(HandStress::getA2).collect(Collectors.toList());
        List<Double> a3 = data.stream().map(HandStress::getA3).collect(Collectors.toList());
        List<Double> a4 = data.stream().map(HandStress::getA4).collect(Collectors.toList());
        List<Double> a5 = data.stream().map(HandStress::getA5).collect(Collectors.toList());
        List<Double> a6 = data.stream().map(HandStress::getA6).collect(Collectors.toList());
        List<Double> a7 = data.stream().map(HandStress::getA7).collect(Collectors.toList());
        List<Double> a8 = data.stream().map(HandStress::getA8).collect(Collectors.toList());
        //调用工具类进行曲线拟合，得出函数的集合点
        List<Double> fitting = mathUtil.fitting(data);
        ArrayList<Double> fittings = new ArrayList<>();
        //用最小二乘法判断拟合程度
        fittings.add(mathUtil.leastSquares(a0,fitting)) ;
        fittings.add(mathUtil.leastSquares(a1,fitting)) ;
        fittings.add(mathUtil.leastSquares(a2,fitting)) ;
        fittings.add(mathUtil.leastSquares(a3,fitting)) ;
        fittings.add(mathUtil.leastSquares(a4,fitting)) ;
        fittings.add(mathUtil.leastSquares(a5,fitting)) ;
        fittings.add(mathUtil.leastSquares(a6,fitting)) ;
        fittings.add(mathUtil.leastSquares(a7,fitting)) ;
        fittings.add(mathUtil.leastSquares(a8,fitting)) ;
        //比较优略性
        return Result.success("最小二乘法计算优略",leastSquares,fitting);
    }

    @Override
    public Result execlRead(InputStream uploadStream) {
        ExeclUtil.readExeclByStream(uploadStream,HandStress.class);
        return Result.success("uploadSuccess",successUpload);
    }

    @Override
    public Result autoReadExecl() {
        String fileName = execlName + dateTime;
        File file = new File("C:\\Users\\86134\\Desktop\\软件\\FSR薄膜压力传感器资料包v6.5\\常用工具软件\\【本店】BLE控制器电脑端测试软件");
        File[] files = file.listFiles();
        if(files.length < 1){
           throw new BusinessException("当前目录下没有文件",BUISNEES_FIND_NULL);
        }
        for (File file1 : files){
            if (file1.getName().contains(fileName)){
                ExeclUtil.readExeclByPath(file1.getPath(),HandStress.class);
            }
        }
        return Result.success("successFind",successFind);
    }

    @Override
    public Result queryAll() {
        List<HandStress> list = list();
        return Result.success("successFind",successFind,list);
    }
}
